#include "../proto/user.pb.h"
#include "SrpcApplication.h"
#include "SrpcProvider.h"
#include <google/protobuf/descriptor.h>
#include <google/protobuf/service.h>
#include <google/protobuf/stubs/callback.h>
#include <iostream>
#include <string>
using namespace std;
using namespace Suser;
using google::protobuf::Closure;
using google::protobuf::RpcController;

class UserService : public Suser::UserServiceRpc {
public:
  void Login(RpcController *controller, const LoginRequest *request,
             LoginResponse *response, Closure *done) override {
    // 从请求头中获取请求内容
    string name = request->name();
    string pwd = request->pwd();
    cout << "name: " << name << " pwd: " << pwd << endl;
    ResultCode *code = response->mutable_result();
    code->set_errorcode(0);
    code->set_errormessage("");
    response->set_success(true);
    done->Run();
  }

  void Register(RpcController *controller, const RegisterRequest *request,
                RegisterResponse *response, Closure *done) override {
    int id = request->id();
    string name = request->name();
    string pwd = request->pwd();
    cout << "id: " << id << " name: " << name << " pwd: " << pwd;
    ResultCode *code = response->mutable_result();
    code->set_errorcode(0);
    code->set_errormessage("");
    response->set_success(true);
    done->Run();
  }
};

int main(int argc, char **argv) {
  SrpcApplication::Init(argc, argv);
  SrpcProvider provider;
  provider.RegistService(new UserService());
  // 启动服务
  provider.Run();
  return 0;
}